package boundary;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import control.BoardEngine;
import control.DiceGenerator;

public class BoardWindow extends JPanel implements ActionListener
{
	ImagePanel[] box;
	JLabel[] players; //chess
	Icon[] playerIcons; //hold a list of player's icons 
	JButton btnRoll;
	boolean btnState;
	DiceGenerator dice;
	JLabel diceValue;
	int playerNo;
	int[] playerPos;
	BoardEngine boardEngine;
	
	public BoardWindow(Icon[] playerIcons, int playerNo, BoardEngine BE)
	{
		this.playerIcons=playerIcons;
		this.playerNo=playerNo;
		this.boardEngine=BE;
		this.initBoard();
		this.initDiceRoll();
	}
	
	public void initPlayerPos(int playerNo)
	{		
		this.playerNo=playerNo;
		this.playerPos=new int[this.playerNo];
		for(int i=0;i<this.playerNo;i++)
			this.playerPos[i]=0;
		
		System.out.println("boardWindow--initPlayerPos : "+this.playerNo);
		players=new JLabel[this.playerNo];
		
		for(int i=0;i<this.playerNo;i++)
		{
			players[i]=new JLabel();
			players[i].setIcon(this.playerIcons[i]);
			box[0].add(players[i]);
		}
	}

	private void initBoard()
	{
		/*-- one board holds 28 boxes --*/
		this.box=new ImagePanel[28];
		int x,y;
		int width=80, height=80;
		
		/*-- init each box of the Board --*/
		for(int i=0;i<28;i++)
		{
			box[i] = new ImagePanel(new ImageIcon("images\\Map_Normal.gif").getImage());
			box[i].setLayout(new FlowLayout());
			box[i].setBorder(new LineBorder(new Color(0, 0, 0)));
		}
		box[0].setBGImage(new ImageIcon("images\\Map_Start.gif").getImage());
		/*-- init position of the boxes --*/
		x=40;y=4;
		for(int i=0;i<9;i++,x+=80)
			box[i].setBounds(x,4,width,height);
		
		x=680;y=84;
		for(int i=9;i<14;i++,y+=80)
			box[i].setBounds(x,y,width,height);
		
		x=40;y=84;
		for(int i=27;i>22;i--,y+=80)
			box[i].setBounds(x,y,width,height);
		
		x=40;y=484;
		for(int i=22;i>13;i--,x+=80)
			box[i].setBounds(x,y,width,height);
		
		/*-- add boxes to Board --*/
		for(int i=0;i<28;i++)
			this.add(box[i]);
	}
	private void initDiceRoll()
	{
		dice = new DiceGenerator();
		btnRoll = new JButton("roll!");
		btnRoll.addActionListener(this);
		dice.setBounds(360,244,70,70);
		btnRoll.setBounds(360,324,70,30);
		this.add(this.dice);
		this.add(this.btnRoll);
	}
	public void initBoxType(String type,int[] boxList)
	{	
		JLabel[] boxLabel=new JLabel[boxList.length];
		for(int i=0;i<boxList.length;i++)
		{
			boxLabel[i]=new JLabel(type);
			boxLabel[i].setVisible(false);
			this.box[boxList[i]].add(boxLabel[i]);
			
			if(type=="Event")
				this.box[boxList[i]].setBGImage(new ImageIcon("images\\Map_Event.gif").getImage());
			else if(type=="SA")
				this.box[boxList[i]].setBGImage(new ImageIcon("images\\Map_SA.gif").getImage());
		}
	}
	public int rollDice()
	{
		this.dice.rollDice();
		int step=this.dice.getDiceNo();
		this.btnState=true;
		repaint();
		return step;
	}
	public void resetBtnState()
	{
		this.btnState=false;
	}
	
	public void enableRollBtn(boolean enable)
	{
		this.btnRoll.setEnabled(enable);
	}
	
	public void updatePlayerPos(int[] playerPos)
	{
		this.playerPos=playerPos;
		
		for(int i=0; i<playerNo;i++)
			box[playerPos[i]].add(players[i]);
	}
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource()==btnRoll)
		{
			this.boardEngine.startRound();
			this.btnRoll.setEnabled(false);
		}
	}
}
